package com.example.guava;

import com.google.common.cache.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.TimeUnit;

@SpringBootApplication
public class GuavaApplication {

    public static void main(String[] args) throws InterruptedException {
//        SpringApplication.run(GuavaApplication.class, args);

        Cache<String, String> testPhone = CacheBuilder.newBuilder()
                //设置cache的初始大小为10，要合理设置该值
                .initialCapacity(5000)
                //设置并发数为5，即同一时间最多只能有5个线程往cache执行写入操作
                .concurrencyLevel(16)
                .maximumSize(100000)
                //设置cache中的数据在写入之后的存活时间为10秒
                .expireAfterAccess(10, TimeUnit.DAYS)
                //构建cache实例
                .build();
        /*Cache<String, String> testBankAccount = CacheBuilder.newBuilder()
                //设置cache的初始大小为10，要合理设置该值
                .initialCapacity(5000)
                //设置并发数为5，即同一时间最多只能有5个线程往cache执行写入操作
                .concurrencyLevel(16)
                .maximumSize(10000)
                //设置cache中的数据在写入之后的存活时间为10秒
                .expireAfterAccess(10, TimeUnit.DAYS)
                //构建cache实例
                .build();

        Cache<String, String> testBvnt = CacheBuilder.newBuilder()
                //设置cache的初始大小为10，要合理设置该值
                .initialCapacity(5000)
                //设置并发数为5，即同一时间最多只能有5个线程往cache执行写入操作
                .concurrencyLevel(16)
                .maximumSize(10000)
                //设置cache中的数据在写入之后的存活时间为10秒
                .expireAfterAccess(10, TimeUnit.DAYS)
                //构建cache实例
                .build();

        Cache<String, String> testBackCard = CacheBuilder.newBuilder()
                //设置cache的初始大小为10，要合理设置该值
                .initialCapacity(5000)
                //设置并发数为5，即同一时间最多只能有5个线程往cache执行写入操作
                .concurrencyLevel(16)
                .maximumSize(10000)
                //设置cache中的数据在写入之后的存活时间为10秒
                .expireAfterAccess(10, TimeUnit.DAYS)
                //构建cache实例
                .build();*/

        long start = System.currentTimeMillis();
        System.out.println(System.currentTimeMillis());
        for (int i = 0; i< 5000; i ++) {
            testPhone.put("testPhone" + i, "phonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephone" + i);
//            testBackCard.put("testBackCard" + i, "phonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephone" + i);
//            testBankAccount.put("testBankAccount" + i, "phonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephone" + i);
//            testBvnt.put("testBvnt" + i, "phonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephonephone" + i);
        }
        System.out.println(System.currentTimeMillis());
        System.out.println((System.currentTimeMillis() - start));

        Thread.sleep(12000);

        System.out.println(testPhone.getIfPresent("testPhone1"));
        System.out.println(testPhone.getIfPresent("testPhone1"));

        Thread.sleep(1000000000000L);



    }

}
